package com.toobigtofail.entities
{
	import com.toobigtofail.drum.DrumListener;
	import com.toobigtofail.drum.HitResult;
	import com.toobigtofail.drum.Notes;
	import com.toobigtofail.drum.Pattern;
	
	import flash.events.KeyboardEvent;
	
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.Graphic;
	import net.flashpunk.Mask;
	import net.flashpunk.graphics.Graphiclist;
	import net.flashpunk.graphics.Text;
	import net.flashpunk.utils.Draw;
	
	public class DrumTest extends Entity
	{
		private var _pattern:Pattern;
		private var _status:String = "LET'S DRUM!";
		private var _tempoThreshold:Number = 300;
		
		private var _drumListener:DrumListener = new DrumListener;
		
		public function DrumTest(x:Number=0, y:Number=0, pattern:String=null) {
			if (null == pattern) {
				// for testing
				pattern = "A.A.A|B.BB|C.CC";
			}
			_pattern = new Pattern(pattern, 30);
			
			this.x = x;
			this.y = y;
		}
		
		public function setDrums(...notes):Array {
			return _drumListener.setDrums(Vector.<int>(notes));
		}
		
		override public function added():void {
			super.added();
			
			var statusText:Text = new Text(_status);
			var nextNoteText:Text = new Text("Next Note: " + Notes.toString(_pattern.nextNote), 0, 20);
			
			graphic = new Graphiclist(statusText, nextNoteText);
			
			_pattern.start();
			_drumListener.start();
		}
		
		override public function update():void {
			super.update();
			
			var drumsHit:Array = _drumListener.getDrumsHit();
			for each (var key:int in drumsHit) {
				drum(key);
			}
		}
		
		override public function render():void {
			super.render();
			
			var wp:Number = Math.max(0, (_pattern.nextDrumTime - (new Date).getTime()) / _pattern.millisBetweenBeats);
			
			Draw.rect(x+0, y+75, wp*150, 25);
		}
		
		public function drum(key:int):void {
			var hitResult:HitResult = _pattern.checkNextNote(key);
			
			var textColor:uint = 0xFFFFFF;
			if (Math.abs(hitResult.timingOffset) > _tempoThreshold) {
				textColor = 0xFF0000;
				hitResult.result = HitResult.FAILURE;
			}
			
			switch(hitResult.result) {
				case HitResult.NOTE_SUCCESS:    _status = "KEEP THE BEAT!"; break;
				case HitResult.MEASURE_SUCCESS: _status = "I'M GETTING INTO IT NOW!"; break;
				case HitResult.PATTERN_SUCCESS: _status = "WHOA! A PRODIGY, EH?"; break;
				case HitResult.FAILURE: 		_status = "ASSHOLE..."; 
												_pattern.reset();
												textColor = 0xFF0000;
												break;
			}
			
			var nextNote:String = Notes.toString(_pattern.nextNote);
			
			var statusText:Text = new Text(_status);
			statusText.color = textColor;
			var nextNoteText:Text = new Text("Next Note: " + nextNote, 0, 20);
			nextNoteText.color = textColor;
			var offsetText:Text = new Text("Off by " + (hitResult.timingOffset/1000) + " seconds", 0, 40);
			offsetText.color = textColor;
			
			graphic = new Graphiclist(statusText, nextNoteText, offsetText);
		}
	}
}